<template>
	<view class="back-view">
		<view class="multi-selector-back" v-for="(item, index) in contents" :key="index">
			<view class="multi-selector-back-item">
				<uni-icons v-if="item.selected == null || item.selected == false" :type="leftIcon" :color="color"
					size="16" @click="oneClick(item,index)" />
				<uni-icons v-else :type="bottomIcon" :color="color"  size="16" @click="oneClick(item,index)" />
				<view class="multi-selector-back-item-title" @click="onClick(item)">{{item[titleKey]}}</view>
			</view>
			<view v-if="item.selected !== null && item.selected == true" class="multi-selector-back" v-for="(item1, index1) in item[childsKey]" :key="index1">
				<view class="multi-selector-back-item">
					<uni-icons v-if="item1.selected == null || item1.selected == false" :type="leftIcon"
						:color="color" size="16" @click="twoClick(item1,index,index1)" />
					<uni-icons v-else :type="bottomIcon" :color="color" size="16"
						@click="twoClick(item1,index,index1)" />
					<view class="multi-selector-back-item-title" @click="onClick(item1)">{{item1[titleKey]}}</view>
				</view>
				<view v-if="item1.selected !== null && item1.selected == true" class="multi-selector-back" v-for="(item2, index2) in item1[childsKey]" :key="index2">
					<view class="multi-selector-back-item">
						<view class="multi-selector-back-item-title"  @click="onClick(item2)">{{item2[titleKey]}}</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "SxyMultiSelector",
		emits: ['click'],
		data() {
			return {
				color: '#222222',
				leftIcon: 'left',
				bottomIcon: 'bottom',
				contents:[]
			};
		},
		props: {
			datas: {
				type: Array,
				default: []
			},
			titleKey: {
				type: String,
				default: 'title'
			},
			childsKey: {
				type: String,
				default: 'childs'
			}
		},
		watch: {
			contents(newVal, oldVal) {
			  this.$set(this, 'contents', newVal)
			}
		},
		created() {
			let datas = this.datas
			this.contents = datas
		},
		methods: {
			onClick(item) {
				this.$emit('click', item)
			},
			oneClick(item, index) {
				if (item.selected == null || item.selected == false) {
					this.contents[index].selected = true
				} else {
					this.contents[index].selected = false
				}
				this.$forceUpdate()
			},
			twoClick(item, index, index1) {
				var dataItem = this.contents[index]
				if (item.selected == null || item.selected == false) {
					item.selected = true
				} else {
					item.selected = false
				}
				dataItem[this.childsKey][index1] = item
				this.contents[index] = dataItem
				this.$forceUpdate()
			},
			threeClick(item, index, index1, index2) {
				var dataItem = this.contents[index]
				var dataItem2 = dataItem[this.childsKey][index1]
				if (item.selected == null || item.selected == false) {
					item.selected = true
				} else {
					item.selected = false
				}
				dataItem2[this.childsKey][index2] = item
				dataItem[this.childsKey][index1] = dataItem2
				this.contents[index] = dataItem
				this.$forceUpdate()
			}
		}
	}
</script>

<style>
	
	.back-view {
		width: 100%;
	}
	
	.multi-selector-back {
		padding-left: 20px;
		display: flex;
		flex-direction: column;
		justify-content: flex-start;
		align-items: center;
		width: 100%;
	}

	.multi-selector-back-item {
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		align-items: center;
		width: 100%;
		height: 30px;
	}

	.multi-selector-back-item-title {
		width: 100%;
		color: #222;
		font-size: 14upx;
	}
</style>